package net.wanho.servlet;

import com.alibaba.fastjson.JSON;
import net.wanho.constant.Constants;
import net.wanho.entity.User;
import net.wanho.util.AjaxResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Author：汤小洋
 * Date：2025-06-16 10:29
 * Description：<描述>
 */
@WebServlet("/welcome")
public class MyServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        System.out.println("欢迎来到Servlet....." + username);

        /**
         * 允许跨域访问
         */
        resp.setHeader("Access-Control-Allow-Origin", "*");

        // resp.setContentType("text/plain; charset=utf8");
        resp.setContentType("application/json; charset=utf8");
        PrintWriter out = resp.getWriter();

        User user = new User();
        user.setName(username);
        user.setPwd("123456");
        user.setAge(18);
        user.setSex("男");
        user.setHeight(1.80);
        user.setBirthday(new java.util.Date());

        // String jsonString = JSON.toJSONString(user);
        // System.out.println(jsonString);

        /**
         * 成功
         */
        // AjaxResult result = new AjaxResult();
        // result.setStatus(1);
        // result.setMsg("success");
        // result.setData(user);

        // AjaxResult result = AjaxResult.success(1, "success", user);

        /**
         * 失败
         */
        // AjaxResult result = new AjaxResult();
        // result.setStatus(2);
        // result.setMsg("未找到对应的数据");


        // out.print(JSON.toJSONString(AjaxResult.fail(Constants.FAIL, "未找到对应数据")));
        out.print(JSON.toJSONString(AjaxResult.success(user)));
        out.close();
    }
}
